{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Content-Type': 'application/json',\n",
       " 'Ocp-Apim-Subscription-Key': 'tbdIripRr96cUlAGKFHtFroB'}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "KEY = 'tbdIripRr96cUlAGKFHtFroB'  # Replace with a valid Subscription Key here.\n",
    "\n",
    "# 沿用API的示范代码，{subscription key}用KEY代入\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': 'tbdIripRr96cUlAGKFHtFroB'.format(subscription_key = KEY),\n",
    "}\n",
    "headers\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "from aip import AipFace\n",
    "\n",
    "\"\"\" 你的 APPID AK SK \"\"\"\n",
    "APP_ID = '14131125'\n",
    "API_KEY = 'tbdIripRr96cUlAGKFHtFroB'\n",
    "SECRET_KEY = 'Znc0y2Yh4SpMXuBL7I5AdTzuiXcWcdNh'\n",
    "\n",
    "client = AipFace(APP_ID, API_KEY, SECRET_KEY)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "#from 模块名 import 函数名\n",
    "from aip import AipFace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'cached': 0,\n",
       " 'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 744193243578369711,\n",
       " 'result': {'face_list': [{'age': 25,\n",
       "    'angle': {'pitch': 1.94, 'roll': 5.34, 'yaw': 1.31},\n",
       "    'face_probability': 1,\n",
       "    'face_token': '99780525abfaddc5a333e7d86f010611',\n",
       "    'gender': {'probability': 1, 'type': 'female'},\n",
       "    'location': {'height': 154,\n",
       "     'left': 320.42,\n",
       "     'rotation': 6,\n",
       "     'top': 223.25,\n",
       "     'width': 167},\n",
       "    'race': {'probability': 1, 'type': 'yellow'}},\n",
       "   {'age': 24,\n",
       "    'angle': {'pitch': 3, 'roll': 9.99, 'yaw': -4.29},\n",
       "    'face_probability': 1,\n",
       "    'face_token': '0b8cb88c0982fd9433577c90e42dfdfd',\n",
       "    'gender': {'probability': 1, 'type': 'female'},\n",
       "    'location': {'height': 154,\n",
       "     'left': 659.22,\n",
       "     'rotation': 12,\n",
       "     'top': 247.35,\n",
       "     'width': 158},\n",
       "    'race': {'probability': 1, 'type': 'yellow'}},\n",
       "   {'age': 19,\n",
       "    'angle': {'pitch': 5.95, 'roll': -5.74, 'yaw': 3.84},\n",
       "    'face_probability': 1,\n",
       "    'face_token': '4d6f5d59673c823e74fad37573be604f',\n",
       "    'gender': {'probability': 1, 'type': 'female'},\n",
       "    'location': {'height': 141,\n",
       "     'left': 1148.6,\n",
       "     'rotation': -2,\n",
       "     'top': 373.57,\n",
       "     'width': 160},\n",
       "    'race': {'probability': 1, 'type': 'yellow'}},\n",
       "   {'age': 26,\n",
       "    'angle': {'pitch': 16.59, 'roll': -7.46, 'yaw': 8.49},\n",
       "    'face_probability': 1,\n",
       "    'face_token': '76aaa36e41ec02ae174ef420a9a7d453',\n",
       "    'gender': {'probability': 1, 'type': 'female'},\n",
       "    'location': {'height': 138,\n",
       "     'left': 882.33,\n",
       "     'rotation': -1,\n",
       "     'top': 344.39,\n",
       "     'width': 149},\n",
       "    'race': {'probability': 1, 'type': 'yellow'}}],\n",
       "  'face_num': 4},\n",
       " 'timestamp': 1544357836}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#选择了URL类型，base64看不懂。暂时用一张图片链接测试（图片的base64编码指将一副图片数据编码成一串字符串，使用该字符串代替图像地址。）\n",
    "image = \"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1544208974338&di=2e86f9cd0e74b20e7a018d50e69d509f&imgtype=0&src=http%3A%2F%2Fs4.sinaimg.cn%2Forignal%2F492dc5e9bdaf7ac9bab73\"\n",
    "imageType = \"URL\"\n",
    "\n",
    "#client.detect()这个函数即为面部检测函数，内部可填入三个参数，image,imageType,options。\n",
    "\"\"\" 调用人脸检测 \"\"\"\n",
    "client.detect(image, imageType);\n",
    "\n",
    "\n",
    "\"\"\" 如果有可选参数 \"\"\"\n",
    "options = {}\n",
    "options[\"face_field\"] = \"race,age,gender\"\n",
    "options[\"max_face_num\"] = 4\n",
    "options[\"face_type\"] = \"LIVE\"\n",
    "\n",
    "\"\"\" 带参数调用人脸检测 \"\"\"\n",
    "client.detect(image, imageType, options)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'log_id': 747956943578375981, 'error_code': 0, 'result': {'face_num': 4, 'face_list': [{'face_probability': 1, 'angle': {'pitch': 1.94, 'yaw': 1.31, 'roll': 5.34}, 'face_token': '99780525abfaddc5a333e7d86f010611', 'location': {'height': 154, 'rotation': 6, 'left': 320.42, 'top': 223.25, 'width': 167}, 'gender': {'probability': 1, 'type': 'female'}, 'race': {'probability': 1, 'type': 'yellow'}, 'age': 25}, {'face_probability': 1, 'angle': {'pitch': 3, 'yaw': -4.29, 'roll': 9.99}, 'face_token': '0b8cb88c0982fd9433577c90e42dfdfd', 'location': {'height': 154, 'rotation': 12, 'left': 659.22, 'top': 247.35, 'width': 158}, 'gender': {'probability': 1, 'type': 'female'}, 'race': {'probability': 1, 'type': 'yellow'}, 'age': 24}, {'face_probability': 1, 'angle': {'pitch': 5.95, 'yaw': 3.84, 'roll': -5.74}, 'face_token': '4d6f5d59673c823e74fad37573be604f', 'location': {'height': 141, 'rotation': -2, 'left': 1148.6, 'top': 373.57, 'width': 160}, 'gender': {'probability': 1, 'type': 'female'}, 'race': {'probability': 1, 'type': 'yellow'}, 'age': 19}, {'face_probability': 1, 'angle': {'pitch': 16.59, 'yaw': 8.49, 'roll': -7.46}, 'face_token': '76aaa36e41ec02ae174ef420a9a7d453', 'location': {'height': 138, 'rotation': -1, 'left': 882.33, 'top': 344.39, 'width': 149}, 'gender': {'probability': 1, 'type': 'female'}, 'race': {'probability': 1, 'type': 'yellow'}, 'age': 26}]}, 'error_msg': 'SUCCESS', 'timestamp': 1544357837, 'cached': 0}\n"
     ]
    }
   ],
   "source": [
    "result = client.detect(image, imageType, options)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "#然后测试base64类型的人脸检测\n",
    "import base64\n",
    "filePath =\"biyezhao.jpg\"\n",
    "with open(filePath,\"rb\") as f:  \n",
    "# b64encode是编码，这里改了一下将二进制格式转化为BASE64格式 要注意不然要报错（_io.BufferedReader object错误）的 ；还有记得把照片和py档放一起\n",
    "    base64_data = base64.b64encode(f.read())\n",
    "image = str(base64_data,'utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'log_id': 747956943578388341, 'error_code': 0, 'result': {'face_num': 4, 'face_list': [{'age': 25, 'angle': {'pitch': 1.94, 'yaw': 1.31, 'roll': 5.34}, 'face_probability': 1, 'face_token': '99780525abfaddc5a333e7d86f010611', 'location': {'height': 154, 'rotation': 6, 'left': 320.42, 'top': 223.25, 'width': 167}, 'beauty': 25.15}, {'age': 24, 'angle': {'pitch': 3, 'yaw': -4.29, 'roll': 9.99}, 'face_probability': 1, 'face_token': '0b8cb88c0982fd9433577c90e42dfdfd', 'location': {'height': 154, 'rotation': 12, 'left': 659.22, 'top': 247.35, 'width': 158}, 'beauty': 36.7}, {'age': 19, 'angle': {'pitch': 5.95, 'yaw': 3.84, 'roll': -5.74}, 'face_probability': 1, 'face_token': '4d6f5d59673c823e74fad37573be604f', 'location': {'height': 141, 'rotation': -2, 'left': 1148.6, 'top': 373.57, 'width': 160}, 'beauty': 32.66}, {'age': 26, 'angle': {'pitch': 16.59, 'yaw': 8.49, 'roll': -7.46}, 'face_probability': 1, 'face_token': '76aaa36e41ec02ae174ef420a9a7d453', 'location': {'height': 138, 'rotation': -1, 'left': 882.33, 'top': 344.39, 'width': 149}, 'beauty': 37.84}]}, 'error_msg': 'SUCCESS', 'timestamp': 1544357838, 'cached': 0}\n"
     ]
    }
   ],
   "source": [
    "#参考https://blog.csdn.net/qq_40821981/article/details/81665322\n",
    "from aip import AipFace\n",
    "import base64\n",
    "\"\"\" 你的 APPID AK SK \"\"\"\n",
    "from aip import AipFace\n",
    "\n",
    "\"\"\" 你的 APPID AK SK \"\"\"\n",
    "APP_ID = '14131125'\n",
    "API_KEY = 'tbdIripRr96cUlAGKFHtFroB'\n",
    "SECRET_KEY = 'Znc0y2Yh4SpMXuBL7I5AdTzuiXcWcdNh'\n",
    "\n",
    "client = AipFace(APP_ID, API_KEY, SECRET_KEY)\n",
    "\n",
    "\n",
    "client = AipFace(APP_ID, API_KEY, SECRET_KEY)\n",
    "\n",
    "filePath =\"biyezhao.jpg\"\n",
    "with open(filePath,\"rb\") as f:  \n",
    "# b64encode是编码\n",
    "    base64_data = base64.b64encode(f.read())\n",
    "image = str(base64_data,'utf-8')\n",
    "imageType = \"BASE64\"\n",
    "\n",
    "#参数设置\n",
    "options = {}\n",
    "options[\"face_field\"] = \"age,beauty\"\n",
    "options[\"max_face_num\"] = 4\n",
    "options[\"face_type\"] = \"LIVE\"\n",
    "\n",
    "\"\"\" 调用人脸检测 \"\"\"\n",
    "result = client.detect(image, imageType,options);\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'log_id': 747956943578393141, 'error_code': 222207, 'result': None, 'error_msg': 'match user is not found', 'timestamp': 1544357839, 'cached': 0}\n"
     ]
    }
   ],
   "source": [
    "#测试建一个人脸库 有点复杂 摸索中..\n",
    "from aip import AipFace\n",
    "import base64\n",
    "\"\"\" 你的 APPID AK SK \"\"\"\n",
    "APP_ID = '14131125'\n",
    "API_KEY = 'tbdIripRr96cUlAGKFHtFroB'\n",
    "SECRET_KEY = 'Znc0y2Yh4SpMXuBL7I5AdTzuiXcWcdNh'\n",
    "\n",
    "client = AipFace(APP_ID, API_KEY, SECRET_KEY)\n",
    "\n",
    "\n",
    "filePath ='h1.jpg'\n",
    "\n",
    "with open(filePath,\"rb\") as f:  \n",
    "\n",
    "# b64encode是编码\n",
    "    base64_data = base64.b64encode(f.read())\n",
    "image = str(base64_data,'utf-8')\n",
    "imageType = \"BASE64\"\n",
    "\n",
    "groupIdList = \"test\"\n",
    "\n",
    "\"\"\" 调用人脸搜索 \"\"\"\n",
    "a = client.search(image, imageType, groupIdList);\n",
    "\n",
    "print(a)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "#'error_code': 222207   \tuid format error\t参数格式错误\t参考API说明文档，修改参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
